<?php
/**
 * 小区
 */

namespace app\admin\controller;


use app\admin\model\HouseVillage as HouseVillageModel;
use app\admin\model\HotVallige as HotValligeModel;
use app\admin\model\Region as RegionModel;
use app\common\controller\AdminController;
use app\common\lib\RespCode;
use think\Exception;
use think\facade\Request;

class HouseVillage extends AdminController {

    public $levelConfig = [
        1 => ['getlist', 'getinfo', 'searchname', 'selecthvid','addget','editget'],
        2 => ['sethot','cancelhot', 'addpost', 'editpost', 'deleted'],
        4 => [],
        8 => []
    ];

    public $resubmitLock = ['sethot','cancelhot', 'addPost', 'editPost', 'deleted'];

    /**
     * 小区列表
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            HouseVillageModel::vallige_list($params, $page, $pageNum, $data, $count);
            return $this->resData('success', compact('page', 'pageNum', 'count', 'data'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 设置热门状态
     * @return false|string
     */
    public function setHot() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $re = HotValligeModel::hotStatus($params,1);
            if (!$re) {
                return $this->resErr();
            } else {
                return $this->resData('编辑成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 取消热门状态
     * @return false|string
     */
    public function cancelHot() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $re = HotValligeModel::hotStatus($params,0);
            if (!$re) {
                return $this->resErr();
            } else {
                return $this->resData('编辑成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 详情
     * @return false|string
     */
    public function getInfo() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $re = HouseVillageModel::getInfo($params);
            if (!$re) {
                return $this->resErr();
            }
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 新增获取
     * @return false|string
     */
    public function addGet() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $list = RegionModel::areabusiness($params);
            return $this->resData('success', $list);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 新增提交
     * @return false|string
     */
    public function addPost() {
        try {
            $params = $this->checkParams('area_code,address,name,lng,lat');
            if ($params === false) return $this->resErr();
            $re = HouseVillageModel::edit($params);
            if (!$re) {
                return $this->resErr();
            } else {
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 编辑获取
     * @return false|string
     */
    public function editGet() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $data = HouseVillageModel::where('id', $params['id'])
                ->field('name,price_unit_avg,source_url,summary,property,cost,total_build,person_num,years,parking_space,plot_ratio,greening_rate,developers,tenement,cover,x(location) as lng,Y(location) as lat,city_code as area_code,business_area as b_id,address,trading_ownership,electricity_type,water_type,heating_type,gas_fee')
                ->find();
            if (empty($data)) {
                return $this->resErr('数据不存在', RespCode::PARAMS_ERR);
            }
            $list = RegionModel::areabusiness($params);
            return $this->resData('success', compact('data', 'list'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 编辑提交
     * @return false|string
     */
    public function editPost() {
        try {
            $params = $this->checkParams('id,area_code,address,name,lng,lat');
            if ($params === false) return $this->resErr();
            $re = HouseVillageModel::edit($params);
            if (!$re) {
                return $this->resErr();
            } else {
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除
     * @return false|string
     */
    public function deleted() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $re = HouseVillageModel::del($params);
            if (!$re) {
                return $this->resErr();
            } else {
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 名称匹配小区
     * @return false|string
     */
    public function searchName() {
        try {
            $params = $this->checkParams('name');
            if ($params === false) return $this->resErr();
            $re = HouseVillageModel::searchName($params);
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 房源补录选择小区
     * @return false|string
     */
    public function selectHvId() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $re = HouseVillageModel::selectHvId($params);
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }
}
